package com.cloudpark.mqtt;

import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttCallback;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;

public class SubCallBack implements MqttCallback {

    private String intanceName = "";
    private int i;

    public SubCallBack(String instanceName, int i) {
        this.intanceName = instanceName;
        this.i = i;
    }

    public void connectionLost(Throwable cause) {
        System.out.println("Connection lost : " + cause.getMessage() + "|" + ((MqttException) cause).getReasonCode()
                + "|" + cause.getCause());
        cause.printStackTrace();
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * org.eclipse.paho.client.mqttv3.MqttCallback#messageArrived(java.lang.
     * String, org.eclipse.paho.client.mqttv3.MqttMessage)
     */
    @Override
    public void messageArrived(String topic, MqttMessage message) throws Exception {
        try {
            System.out.println("Message arrived : " + message.toString() + " on Topic : " + topic
                    + " for instance : " + this.intanceName);
            System.out.println("i am " + i + " client ");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * org.eclipse.paho.client.mqttv3.MqttCallback#deliveryComplete(org.eclipse
     * .paho.client.mqttv3.IMqttDeliveryToken)
     */
    @Override
    public void deliveryComplete(IMqttDeliveryToken token) {
        // TODO Auto-generated method stub
        System.out.println(token.getTopics());
    }

}
